package com.gzy.literature.dao;


import com.gzy.literature.entity.UserWorks;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserWorksDao {

    @Select("SELECT * FROM user_works WHERE user_id = #{userId} AND work_id = #{workId}")
    @Results(id = "userWorksResultMap", value = {
            @Result(property = "userId", column = "user_id"),
            @Result(property = "workId", column = "work_id")
    })
    UserWorks queryByIdAndWorkId(Integer userId, Integer workId);

    @Select("<script>" +
            "SELECT * FROM user_works WHERE 1=1 " +
            "<if test='userWorks.userId != null'> AND user_id = #{userWorks.userId} </if>" +
            "</script>")
    @Results(id = "userWorksResultMapList", value = {
            @Result(property = "userId", column = "user_id"),
            @Result(property = "workId", column = "work_id")
    })
    List<UserWorks> queryAllByCondition(@Param("userWorks") UserWorks userWorks);

    @Select("<script>" +
            "SELECT COUNT(*) FROM user_works WHERE 1=1 " +
            "<if test='userWorks.userId != null'> AND user_id = #{userWorks.userId} </if>" +
            "</script>")
    long count(@Param("userWorks") UserWorks userWorks);

    @Insert("INSERT INTO user_works(user_id, work_id) VALUES(#{userId}, #{workId})")
    // 注意：如果 userId 是自增主键，通常不需要设置 useGeneratedKeys，因为它通常用于数据库生成的主键
    @Options(useGeneratedKeys = false, keyProperty = "userId", keyColumn = "user_id")
    int insert(UserWorks userWorks);

    @Update("<script>" +
            "UPDATE user_works " +
            "<set>" +
            "<if test='userWorks.workId != null'> work_id = #{userWorks.workId}, </if>" +
            "</set>" +
            "WHERE user_id = #{userWorks.userId}" +
            "</script>")
    int update(UserWorks userWorks);

    // 考虑到通常不会仅通过 userId 删除所有作品，可能需要更精细的控制
    // 但为了与 UserFavoritesDao 保持一致，这里保留了这个方法
    @Delete("DELETE FROM user_works WHERE user_id = #{userId}")
    int deleteByUserId(Integer userId);

    // 保留这个更具体的删除方法，因为它在实际应用中可能更常用
    @Delete("DELETE FROM user_works WHERE user_id = #{userId} AND work_id = #{workId}")
    int deleteByIdAndWorkId(Integer userId, Integer workId);
}

